#include <ctype.h>
#include <limits.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <float.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <assert.h>
#include <math.h>

#include <stdexcept>
#include <typeinfo>
#include <algorithm>

#include <expat.h>

#define BUFFSIZE 8192

char Buff[BUFFSIZE];
int Depth;

void
start(void *data, const char *el, const char **attr) 
{
   int i;

   for (i = 0; i < Depth; i++)
     printf("\t");

   printf("%s", el);

   for (i = 0; attr[i]; i += 2) {
     printf(" %s='%s'", attr[i], attr[i + 1]);
   }

   printf("\n");
   Depth++;
} /* End of start handler */


void
end(void *data, const char *el) 
{
   Depth--;
} /* End of end handler */


void
main(int argc, char **argv) 
{
   XML_Parser p = XML_ParserCreate(NULL);

   if (! p) {
     fprintf(stderr, "Couldn't allocate memory for parser\n");
     exit(-1);
   }

   XML_SetElementHandler(p, start, end);
   
   strncpy(Buff, "<html><head></head><body><a target= \"_bla", sizeof(Buff));

   if (! XML_Parse(p, Buff, strlen(Buff), 0)) {
      fprintf(stderr, "Parse error at line %d:\n%s\n",
         XML_GetCurrentLineNumber(p),
         XML_ErrorString(XML_GetErrorCode(p)));
      exit(-1);
   }
   
   printf("\n\n\nPRESS ANY KEY TO CONTINUE\n\n\n");
   getchar();
   
   strncpy(Buff, "nk\" href=\"www.labfan.com\"> this is labfan</a></body></html><html></html>", sizeof(Buff));
   
   if (! XML_Parse(p, Buff, strlen(Buff), 0)) {
      fprintf(stderr, "Parse error at line %d:\n%s\n",
         XML_GetCurrentLineNumber(p),
         XML_ErrorString(XML_GetErrorCode(p)));
      exit(-1);
   }
   
   XML_ParserFree(p);

   printf("\n\n\nPRESS ANY KEY TO EXIT\n\n\n");
   getchar();
}